
''' <summary>
''' Store information about a web radio station.
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class OnlineStation

    Private _url As String
    Private _title As String
    Private _favIconUrl As String
    Private _listIndex As Integer

    ''' <summary>
    ''' Creates a new station.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        _title = "New station"
        _url = "http:///"
        _favIconUrl = ""
        _listIndex = 0
    End Sub

    ''' <summary>
    ''' Creates new station with specified title and play URL.
    ''' </summary>
    ''' <param name="title">Station's human readable title.</param>
    ''' <param name="url">URL to the webpage that has the radio playing.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal title As String, ByVal url As String)
        _url = url
        _title = title
        _favIconUrl = ""
        _listIndex = 0
    End Sub

    ''' <summary>
    ''' Creates new station with specified title and play URL and assign the supplied
    ''' list index for future reference.
    ''' </summary>
    ''' <param name="title">Station's human readable title.</param>
    ''' <param name="url">URL to the webpage that has the radio playing.</param>
    ''' <param name="listIndex">The list index to attach to the station.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal title As String, ByVal url As String, ByVal listIndex As Integer)
        _url = url
        _title = title
        _listIndex = listIndex
        _favIconUrl = ""
        _listIndex = 0
    End Sub

    ''' <summary>
    ''' Gets or sets URL of the webpage that has the radio playing.
    ''' </summary>
    ''' <value>URL of the webpage that has the radio playing.</value>
    ''' <returns>URL of the webpage that has the radio playing.</returns>
    ''' <remarks></remarks>
    Public Property URL() As String
        Get
            Return _url
        End Get
        Set(ByVal value As String)
            _url = value

            'if the url is well-formed and favicon hasn't been assigned anything
            If String.IsNullOrEmpty(_favIconUrl) AndAlso _
                Uri.IsWellFormedUriString(value, UriKind.Absolute) Then

                'construct common favicon url
                Dim oUri = New Uri(value)
                Me.FavIconURL = "http://" + oUri.Host + "/favicon.ico"

            End If
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the title of the station.
    ''' </summary>
    ''' <value>Human-readable title of the station.</value>
    ''' <returns>Title of the station.</returns>
    ''' <remarks></remarks>
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    ''' <summary>
    ''' Associated index of this station in its containing list.
    ''' </summary>
    ''' <value>New index value of the station in its containing list.</value>
    ''' <returns>Current index value of the station in its containing list.</returns>
    ''' <remarks></remarks>
    Public Property ListIndex() As Integer
        Get
            Return _listIndex
        End Get
        Set(ByVal value As Integer)
            _listIndex = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the fav icon URL of this station.
    ''' </summary>
    ''' <value>URL to the favicon of the station.</value>
    ''' <returns>URL to the favicon of the station.</returns>
    ''' <remarks>FavIconURL is automatically computed when URL is updated.</remarks>
    ''' 
    <Xml.Serialization.XmlIgnore()> _
    Public Property FavIconURL() As String
        Get
            Return _favIconUrl
        End Get
        Set(ByVal value As String)
            _favIconUrl = value
        End Set
    End Property

    ''' <summary>
    ''' Compute and return the hash code from the URL and Title of the station.
    ''' </summary>
    ''' <returns>Hash code for this instance computed from the URL and Title.</returns>
    ''' <remarks>Refers to <see cref="String.GetHashCode()">String.GetHashCode()</see> for more information</remarks>
    Public Overrides Function GetHashCode() As Integer

        'compute the hash using String's built-in hash function
        'combining the url and title altogether
        Return (_url + _title).GetHashCode()

    End Function

    ''' <summary>
    ''' Returns the title of the station.
    ''' </summary>
    ''' <returns>Title of the station.</returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return _title
    End Function

End Class
